package ro.tm.uvt.lp.raccoon.catchGame.environment;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ro.tm.uvt.lp.raccoon.catchGame.entities.Entity;
import ro.tm.uvt.lp.raccoon.catchGame.entities.Odor;
import ro.tm.uvt.lp.raccoon.catchGame.entities.agents.Dog;
import ro.tm.uvt.lp.raccoon.catchGame.entities.agents.Raccoon;
import ro.tm.uvt.lp.raccoon.catchGame.entities.perceptions.CookieOdor;
import ro.tm.uvt.lp.raccoon.catchGame.entities.physicalObjects.Burrow;
import ro.tm.uvt.lp.raccoon.catchGame.entities.physicalObjects.Food;
import ro.tm.uvt.lp.raccoon.catchGame.entities.physicalObjects.NatureStaticEntity;
import ro.tm.uvt.lp.raccoon.catchGame.utils.RandomIntGenerator;

/**
 * Created by user on 1/14/2015.
 */
public enum Environment implements RaccoonDied{

    INSTANCE;

    private int width;
    private int height;
    private int tileWidth;
    private int tileHeight;

    private Raccoon raccoon;
    private Burrow burrow;

    private ArrayList<Cell> cells = new ArrayList<Cell>();
    private ArrayList<Dog> dogs=new ArrayList<Dog>();
    private ArrayList<NatureStaticEntity> nature=new ArrayList<NatureStaticEntity>();
    private ArrayList<Food> foods=new ArrayList<Food>();
//    private ArrayList<Odor> odorList=new ArrayList<Odor>();

    private int numberOfDogs=3;// RandomIntGenerator.randInt(1,2);
    private int numberOfFood=12;

    public void createNature(){
        NatureStaticEntity tree1, tree2, tree3, tree4, tree5, tree6, bush1, bush2, bush3, bush4, bush5, rock, rock2, rock3, rock4, rock5, rock6, rock7, rock8;
        rock = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock.setPosition(0,440);
        this.addEntity(rock);
        this.addNatureItem(rock);

        rock2 = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock2.setPosition(280,120);
        this.addEntity(rock2);
        this.addNatureItem(rock2);

        rock4 = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock4.setPosition(320,440);
        this.addEntity(rock4);
        this.addNatureItem(rock4);

        rock5 = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock5.setPosition(360,440);
        this.addEntity(rock5);
        this.addNatureItem(rock5);

        rock6 = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock6.setPosition(400,440);
        this.addEntity(rock6);
        this.addNatureItem(rock6);

        rock7 = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock7.setPosition(440,440);
        this.addEntity(rock7);
        this.addNatureItem(rock7);

        rock8 = new NatureStaticEntity(new Sprite(new Texture("entities/rock40.png")));
        rock8.setPosition(480,440);
        this.addEntity(rock8);
        this.addNatureItem(rock8);

        tree5 = new NatureStaticEntity(new Sprite(new Texture("entities/tree40.png")));
        tree5.setPosition(0, 0);
        this.addEntity(tree5);
        this.addNatureItem(tree5);

        tree6 = new NatureStaticEntity(new Sprite(new Texture("entities/tree40.png")));
        tree6.setPosition(240, 80);
        this.addEntity(tree6);
        this.addNatureItem(tree6);

        tree1 = new NatureStaticEntity(new Sprite(new Texture("entities/tree40.png")));
        tree1.setPosition(560, 320);
        this.addEntity(tree1);
        this.addNatureItem(tree1);

        tree2 = new NatureStaticEntity(new Sprite(new Texture("entities/tree40.png")));
        tree2.setPosition(680, 0);
        this.addEntity(tree2);
        this.addNatureItem(tree2);

        tree3 = new NatureStaticEntity(new Sprite(new Texture("entities/tree40.png")));
        tree3.setPosition(720, 440);
        this.addEntity(tree3);
        this.addNatureItem(tree3);

        tree4 = new NatureStaticEntity(new Sprite(new Texture("entities/tree40.png")));
        tree4.setPosition(440, 760);
        this.addEntity(tree4);
        this.addNatureItem(tree4);

        bush1 = new NatureStaticEntity(new Sprite(new Texture("entities/bush40.png")));
        bush1.setPosition(640,80);
        this.addEntity(bush1);
        this.addNatureItem(bush1);

        bush2 = new NatureStaticEntity(new Sprite(new Texture("entities/bush40.png")));
        bush2.setPosition(200, 360);
        this.addEntity(bush2);
        this.addNatureItem(bush2);

        bush3 = new NatureStaticEntity(new Sprite(new Texture("entities/bush40.png")));
        bush3.setPosition(600, 320);
        this.addEntity(bush3);
        this.addNatureItem(bush3);

        bush4 = new NatureStaticEntity(new Sprite(new Texture("entities/bush40.png")));
        bush4.setPosition(200, 320);
        this.addEntity(bush4);
        this.addNatureItem(bush4);

        bush5 = new NatureStaticEntity(new Sprite(new Texture("entities/bush40.png")));
        bush5.setPosition(0, 40);
        this.addEntity(bush5);
        this.addNatureItem(bush5);
    }

    /**
     *
     * @param nse
     */
    public void addNatureItem(NatureStaticEntity nse){
        this.nature.add(nse);
    }

    /**
     *
     * @return
     */
    public ArrayList<NatureStaticEntity> getNature(){
        return  this.nature;
    }

    /**
     *
     */
    public void createBurrow(){
        this.burrow = new Burrow(new Sprite(new Texture("entities/rb40.png")));
        this.burrow.setPosition(760,160);
        this.addEntity(burrow);
    }

    /**
     *
     */
    public void createFood(){
        for (int i=0;i<this.numberOfFood;i++){
            Cell cell = Environment.INSTANCE.getAvailableCell();
            Food food=new Food(new Sprite(new Texture("entities/cookie40.png")), cell.getX(),cell.getY());
            this.addEntity(food);
            this.addFood(food);
        }
    }

    /**
     *
     * @return
     */
    public ArrayList<Food> getFoods(){
        return this.foods;
    }

    /**
     *
     * @param food
     */
    public void addFood(Food food){
        this.foods.add(food);
    }

//    public void addOdor(Odor odor){
//        this.odorList.add(odor);
//    }

//    public ArrayList<Odor> getOdorList(){
//        return this.odorList;
//    }

    public void deleteFoodCell(Food food){
        Iterator<Cell> it=this.cells.iterator();
        while(it.hasNext()){
            Cell cell=it.next();
            if(cell.getX()==food.getX() && cell.getY()==food.getY() && cell.getType().equalsIgnoreCase(CellType.FOOD))
                it.remove();
        }
    }


    /**
     *
     * @return
     */
    public ArrayList<Cell> getCells() {
        return cells;
    }

    /**
     *
     * @param nse
     */
    public void addEntity(Entity nse){
        Cell cell=new Cell(nse);
        this.cells.add(cell);
    }

    public void createRaccoon(){
        Cell cell=Environment.INSTANCE.getAvailableCell();
        this.raccoon = new Raccoon(new Sprite(new Texture("entities/raccoon40.png")), cell.getX(),cell.getY());
        this.addEntity(this.raccoon);
    }

    public void createDogs(){
        for(int i=0;i<this.getNumberOfDogs();i++) {
            Cell cell = Environment.INSTANCE.getAvailableCell();
            Dog dog = new Dog(new Sprite(new Texture("entities/basset40.png")), cell.getX(), cell.getY());
            this.addEntity(dog);
            this.addDog(dog);
        }
    }









    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getTileWidth() {
        return tileWidth;
    }

    public void setTileWidth(int tileWidth) {
        this.tileWidth = tileWidth;
    }

    public int getTileHeight() {
        return tileHeight;
    }

    public void setTileHeight(int tileHeight) {
        this.tileHeight = tileHeight;
    }

    public int getNumberOfColumns(){
        return width/tileWidth;
    }

    public int getNumberOfRows(){
        return height/tileHeight;
    }


    public int getNumberOfDogs(){
        return this.numberOfDogs;
    }




    public Raccoon getRaccoon(){
        return this.raccoon;
    }

    public ArrayList<Dog> getDogs(){
        return this.dogs;
    }

    public void addDog(Dog dog){
        this.dogs.add(dog);
    }

    public Burrow getBurrow(){
        return this.burrow;
    }


    private boolean isCellOcupied(int row, int column){

        for(Cell cell:this.cells){
            if(cell.isOcupied(row, column)) return true;
        }
        return false;
    }


    public Cell getAvailableCell(){

        int row=0; int column=0;

        do{
            row=RandomIntGenerator.randInt(1,this.getNumberOfRows()-1);
            column=RandomIntGenerator.randInt(1,this.getNumberOfColumns()-1);
        }while(this.isCellOcupied(row, column));

//        Gdx.app.debug(">>>>>>>>>>>>>>Cell.row and columen computed:: ",row+" "+column);
        Cell cell = new Cell(row, column);

        return cell;
    }

//    public Cell collides(int x, int y){
//        for(Cell cell: this.cells){
//            if((cell.getX()==x || cell.getY()==y)&& !cell.getType().equalsIgnoreCase(CellType.RACCOON))
//                return cell;
//        }
//        return null;
//    }

    @Override
    public void died(){
        this.raccoon.resetVelocity();
        for(Dog dog:this.dogs){
            dog.resetVelocity();
        }
    }

}
